/**  
 * All rights Reserved
 * @Title:  ConcurrentTest.java   
 * @Package com.basic.collection   
 * @Description:    TODO   
 * @author: Joseph Gao     
 * @date:   2018年11月18日 下午8:34:10   
 * @version V1.0 
 * @Copyright: 2018 All rights reserved. 
 */
package com.basic.collection;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import com.spring.beans.User;

/** 
* 
*
* @author Joseph
* @date 2018年11月18日
*/
public class ConcurrentTest {
	/**@Title: main   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @param args      
	 * @return: void      
	 * @throws   
	 */
	public static void main(String[] args) {
		//collectionTest();
		mapTest();

	}
	
	public static void collectionTest() {
		List<User> userLists = new ArrayList<User>();
		userLists.add(new User("zhangsan", 20));
		userLists.add(new User("lisi", 40));
		for (User user : userLists) {
			System.out.println(user.toString());
		}
		
		List<User> userLists2 = new LinkedList<User>(); 
		userLists2.add(new User("zhangsan", 20));
		userLists2.add(new User("lisi", 40));
		for (User user : userLists2) {
			System.out.println(user.toString());
		}
	}
	
	public static void mapTest() {
		Map<String, User> hashtable = new Hashtable<String, User>(); // Thread safe
		ConcurrentHashMap<String, User> concurrentHashMap = new ConcurrentHashMap<String, User>(); // Thread safe
		Map<String, User> map = new HashMap<String, User>(); // unsafe
		map.put("zhangsan", new User("zhangsan", 20));
		map.put("lisi", new User("lisi", 40));
		if (!map.containsKey("zhangsan")) {
			map.put("zhangsan", new User("zhangsan", 20));
		}
		map.put("lisi", new User("lisi", 50));
		for (User user : map.values()){
			System.out.println(user.toString());
		}
		for(String key : map.keySet()) {
			System.out.println(key+":" + map.get(key).toString());
		}
		Iterator<Map.Entry<String, User>> iterator = map.entrySet().iterator();
		while (iterator.hasNext()) {
			Map.Entry<String, User> entry = iterator.next();
			System.out.println(entry.getKey() + ":" + entry.getValue());
		}
		for (Entry<String, User> entry : map.entrySet()) {
			System.out.println(entry.getKey() + ":" + entry.getValue());
		}
	}

}
